package sz.szsmk.citizencard.mvp.presenter;

import com.google.gson.Gson;

import javax.inject.Inject;

import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.PowerCheckOrderReq;
import sz.szsmk.citizencard.model.network.bean.request.PowerGetMac2Req;
import sz.szsmk.citizencard.model.network.bean.request.PowerRechargeReq;
import sz.szsmk.citizencard.model.network.bean.response.PowerCheckOrderRes;
import sz.szsmk.citizencard.model.network.bean.response.PowerGetMac2Res;
import sz.szsmk.citizencard.model.network.bean.response.PowerRechargeRes;
import sz.szsmk.citizencard.mvp.contract.IPowerContract;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.Constant;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/9/7 15:11
 * description:
 */
public class PowerPresenter extends IBasePresenter
        implements IPowerContract.IPowerPresenter<IPowerContract.IPowerView> {


    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public PowerPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }


    @Override
    public void checkOrder(String cardNo, String cardBalance, String accesstoken, String cardTradeNo) {
        PowerCheckOrderReq powerCheckOrderReq = new PowerCheckOrderReq();
        powerCheckOrderReq.setCard_no(cardNo);
        powerCheckOrderReq.setCard_old_balance(cardBalance);
        powerCheckOrderReq.setAccess_token(accesstoken);
        powerCheckOrderReq.setCard_trade_no(cardTradeNo);
        String json = new Gson().toJson(powerCheckOrderReq);
        L.e(Constant.TAG, "termAllVerify----->" + json);
        mNetworkDateSource.termAllVerify(powerCheckOrderReq, new SubscribeResult<PowerCheckOrderRes>() {
            @Override
            protected void onOk(PowerCheckOrderRes response) {
                ((IPowerContract.IPowerView) mView).checkOrderSucc(response);
            }

            @Override
            protected void onFailure(PowerCheckOrderRes response) {
                super.onFailure(response);
                ((IPowerContract.IPowerView) mView).checkOrderFail(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void powerRecharge(String access_token, String cardNo, int mode, int tradeMoney, String prepaid_card_password, String special_account_password) {
        PowerRechargeReq powerRechargeReq = new PowerRechargeReq();
        powerRechargeReq.setAccess_token(access_token);
        powerRechargeReq.setCard_no(cardNo);
        powerRechargeReq.setPay_mode(mode + "");
        powerRechargeReq.setTrade_money(tradeMoney + "");
        powerRechargeReq.setPrepaid_card_password(prepaid_card_password);
        powerRechargeReq.setSpecial_account_password(special_account_password);
        String json1 = new Gson().toJson(powerRechargeReq);
        L.e(Constant.TAG, "powerRecharge--------->" + json1);
        mNetworkDateSource.bluetoothOrder(powerRechargeReq, new SubscribeResult<PowerRechargeRes>() {
            @Override
            protected void onOk(PowerRechargeRes response) {
                String json = new Gson().toJson(response);
                L.e(Constant.TAG, "powerRecharge-----req" + json);
                ((IPowerContract.IPowerView) mView).powerRechargeSucc(response);
            }

            @Override
            protected void onFailure(PowerRechargeRes response) {
                super.onFailure(response);
                ((IPowerContract.IPowerView) mView).powerRechargeFail(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

                ((IPowerContract.IPowerView) mView).powerRechargeFail(throwable.getMessage());
            }
        });

    }

    @Override
    public void powerGetMac2(String access_token, String arithIndex, String asn, String cardNo, int cardOldBal, String cardTradeNo, String cardType, String keyVersion, String mac1, String orderNo, String rndnumber2, int transMoney) {
        PowerGetMac2Req powerGetMac2Req = new PowerGetMac2Req();
        powerGetMac2Req.setAccess_token(access_token);
        powerGetMac2Req.setArith_index(arithIndex);
        powerGetMac2Req.setAsn(asn);
        powerGetMac2Req.setCard_no(cardNo);
        powerGetMac2Req.setCard_old_balance(cardOldBal + "");
        powerGetMac2Req.setCard_trade_no(cardTradeNo);
        powerGetMac2Req.setCard_type(cardType);
        powerGetMac2Req.setKey_version(keyVersion);
        powerGetMac2Req.setMac1(mac1);
        powerGetMac2Req.setOrder_no(orderNo);
        powerGetMac2Req.setRndnumber2(rndnumber2);
        powerGetMac2Req.setSerialNo("");
        powerGetMac2Req.setTrade_money(transMoney);
        powerGetMac2Req.setSupply_time("");
        String json = new Gson().toJson(powerGetMac2Req);
        L.e(Constant.TAG, "powerGetMac2----req" + json);
        mNetworkDateSource.termTopupMac2(powerGetMac2Req, new SubscribeResult<PowerGetMac2Res>() {
            @Override
            protected void onOk(PowerGetMac2Res response) {
                String toJson = new Gson().toJson(response);
                L.e(Constant.TAG, "termTopupMac2---->" + toJson);
                ((IPowerContract.IPowerView) mView).powerGetMac2Succ(response);
            }

            @Override
            protected void onFailure(PowerGetMac2Res response) {
                super.onFailure(response);
                ((IPowerContract.IPowerView) mView).powerGetMac2Fail(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((IPowerContract.IPowerView) mView).powerGetMac2Fail(throwable.getMessage());
            }
        });
    }

}
